<?php
/**
 * Usage
 * DomMobile::loadProfile();
 * DomMobile::getModel(); // 
 * DomMobile::getScreenSize();
 * 
 * @package DomBase
 * @author Oliver Leisalu
 */
class DomMobile {
	
	/**
	 * @var String
	 */
	private static $profileXml;
	/**
	 * @var String
	 */
	private static $model;
	/**
	 * @var String
	 */
	private static $screenSize;
	
	/**
	 * Loads mobile phone profile. 
	 */
	public static function loadProfile() {
		if( self::$profileXml == NULL) {
			$profileUrl = $_SERVER['HTTP_X_WAP_PROFILE'];
			$profileUrl = str_replace('"','', $profileUrl);
			$ch = curl_init( $profileUrl ); 
			//$ch = curl_init('http://nds1.nds.nokia.com/uaprof/NE55-1r100.xml'); // Nokie E55, 240x320
			curl_setopt($ch, CURLOPT_HEADER, 0);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
			self::$profileXml = curl_exec($ch);
			curl_close($ch);
		}
	}
	/**
	 * Mobile phone model number.
	 * @return String
	 */
	public static function getModel() {
		if( self::$model == NULL ) {
			$model = explode('<prf:Model>', self::$profileXml, 2 );
			$model = explode('<', $model[1], 2);
			self::$model = $model[0];
		}
		return self::$model;
	
	}
	/**
	 * Returns mobile device screen size. Retrusn array with two parameters, width and height.
	 * @return Array
	 */
	public static function getScreenSize() {
		if( self::$screenSize == NULL ) {
			$size = explode('<prf:ScreenSize>', self::$profileXml, 2 );
			$size = explode('<', $size[1], 2);
			$size = explode('x',$size[0]);
			self::$screenSize = array('width' => $size[0], 'height' => $size[1]);
		}
		return self::$screenSize;
	}
	
	public static function msisdnToCountry( $msisdn ) {
		$beginning = substr( $msisdn, 0, 3 );
		switch( $beginning ) {
			case '372':
				return 'EE';
			case '371':
				return 'LV';
			case '370':
				return 'LT';
			case '358':
				return 'FI';
			default:
				return false;
		}
	}

}